/* CSS can not pull down an element to the requested height, which is a problem if
* two side-by-side columns can have variable length, and have different background colors
* One solution is to use a filler div, and calculate its height at rendering time.
* The second column is handled by setting the overall background color to the column 
* background color 
*/
function SetLeftFillerHeight() {
    var elemFiller = document.getElementById("leftColFiller");
    var elemTerm = document.getElementById("pageBodyTerminator");

    if (elemFiller && elemTerm) {
        var newHeight = elemTerm.offsetTop - elemFiller.offsetTop - 1;
        if (newHeight >= 0) {
            elemFiller.style.height = newHeight + "px";
        }
    }
}

function MaximizeWindow() {

    var offset = (navigator.userAgent.indexOf("Mac") != -1 ||
                  navigator.userAgent.indexOf("Gecko") != -1 ||







                  navigator.appName.indexOf("Netscape") != -1) ? 0 : 4;
    window.moveTo(-offset, -offset);
    window.resizeTo(screen.availWidth + (2 * offset), screen.availHeight + (2 * offset));
}

function ResetLeftFillerHeight() {
    var elemFiller = document.getElementById("leftColFiller");
    if (elemFiller)
        elemFiller.style.height = 0 + "px";
}

function SetRightFillerHeight() {

    var elemFiller = document.getElementById("rightColFiller");
    var elemTerm = document.getElementById("rightColumnTerminator");
    if (!elemTerm)
        elemTerm = document.getElementById("pageBodyTerminator");
    if (elemFiller && elemTerm) {
        // IE/Opera have rightColumn as offset parent, FF/Netscape have the restOfPage as offset parent.
        // For IE/Opera, calculate extra requried offset
        // TBD: make offset parent-independent, by always resolving relative to body tag (walking up tree)
        var additionalOffsetTop = 0;
        if (elemFiller.offsetParent.id.indexOf('rightColumn') >= 0)
            additionalOffsetTop = elemFiller.offsetParent.offsetTop;

        var newHeight = elemTerm.offsetTop - elemFiller.offsetTop - additionalOffsetTop - 1;

        if (newHeight >= 0) {
            elemFiller.style.height = newHeight + "px";
        }
    }
}

function ResetRightFillerHeight() {
    var elemFiller = document.getElementById("rightColFiller");
    if (elemFiller)
        elemFiller.style.height = 0 + "px";
}


function SetTaglineText() {
    var elemText = document.getElementById("tnTagLineText");
    var elemFooterText = document.getElementById("tnTagLine");
    if (elemText && elemFooterText) {
        elemFooterText.innerHTML = elemText.innerHTML;
        elemText.innerHTML = '';
    }
}


function flashVersion() {
    // Uses swfobject.js
    var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
    var output = playerVersion.major;
    return output;
}


function SetFlashEnabledFlag() {
    var imageAdRotator = document.getElementById("divImageAdRotatorTNOW");
    var flashAdRotator = document.getElementById("divFlashAdRotatorTNOW");
    var useFlashAd = document.getElementById("useTNOWFlashAd");

    //alert("Flash Version : " + flashVersion());
    //alert("Use Flash Ad : " + useFlashAd.value.toString());

    if (flashVersion() >= 10) {
        if (flashAdRotator != null && useFlashAd != null && useFlashAd.value.toString() == "true")
            flashAdRotator.style.display = "block";
        else if (imageAdRotator != null)
            imageAdRotator.style.display = "block";
    }
    else {
        if (flashAdRotator != null)
            flashAdRotator.style.display = "none";
        if (imageAdRotator != null)
            imageAdRotator.style.display = "block";
    }

}


function SetRightFillerHeight() {
}

function OnPageLoad() {
    //alert ( "TNow3Master1"); 
    SetLeftFillerHeight();

    SetRightFillerHeight();

    SetTaglineText();
    //SetSeoTextBlockText();
    SetFlashEnabledFlag();
    SetExchangeSetPriceToolTips();

    try {
        //alert ( "TNow3Master2");
        subMenuInit();
        breadCrumbsInit();
    }
    catch (e)
    { }

    try {
        if (typeof CompleteEnableDisableCheck != 'undefined' && document.getElementById("trPowerSearchZipRow") != null)
            CompleteEnableDisableCheck();
    }
    catch (e)
    { }

}

var timeoutHandle;

function minicartCollapse() {
    var $tvar = jQuery.noConflict();
    if ($tvar("#imgMiniCartExpand").attr('src').indexOf('minicart_minus.gif') >= 0) {
        $tvar("#pnlMiniCartContent").slideToggle(1500);
        $tvar("#imgMiniCartExpand").attr('src', $tvar("#imgMiniCartExpand").attr('src').replace('minicart_minus.gif', 'minicart_plus.gif'));
        clearTimeout(timeoutHandle);
    }
}

function minicartToggle() {
    var $tvar = jQuery.noConflict();
    //alert("minicart toggle");
    clearTimeout(timeoutHandle);
    //$tvar("#pnlMiniCartContent").css("top", $tvar("#mainNav").offset().top + 33);
    $tvar("#pnlMiniCartContent").slideToggle(1500);
    if ($tvar("#imgMiniCartExpand").attr('src').indexOf('minicart_plus.gif') >= 0)
        $tvar("#imgMiniCartExpand").attr('src', $tvar("#imgMiniCartExpand").attr('src').replace('minicart_plus.gif', 'minicart_minus.gif'));
    else
        $tvar("#imgMiniCartExpand").attr('src', $tvar("#imgMiniCartExpand").attr('src').replace('minicart_minus.gif', 'minicart_plus.gif'));
    // collapse after 10 seconds if expanded
    timeoutHandle = setTimeout("minicartCollapse()", 10000);

}


function pageHeight() {
    if (window.innerHeight != null)
        return window.innerWidth;
    else
        if (document.documentElement && document.documentElement.clientHeight)
            return document.documentElement.clientHeight;
        else
            if (document.body != null)
                return document.body.clientHeight;
            else
                return null;
}

function windowScrollLeft() {
    if (typeof window.pageXOffset != 'undefined')
        return window.pageXOffset;
    else
        if (document.documentElement && document.documentElement.scrollLeft)
            return document.documentElement.scrollLeft;
        else
            if (document.body.scrollLeft)
                return document.body.scrollLeft;
            else
                return 0;






}

// Browser support:
// IE: document.documentElement.scrollTop
// FF: document.documentElement.scrollTop + window.pageYOffset
// N:  document.documentElement.scrollTop + window.pageYOffset
// O:  document.documentElement.scrollTop + window.pageYOffset + document.body.scrollTop
function windowScrollTop() {
    if (typeof window.pageYOffset != 'undefined')
        return window.pageYOffset;
    else
        if (document.documentElement && document.documentElement.scrollTop)
            return document.documentElement.scrollTop;
        else
            if (document.body.scrollTop)
                return document.body.scrollTop;
            else
                return 0;
}







function showEventDropdown() {
    var elemEventDropdown = document.getElementById("eventDropdown");
    if (elemEventDropdown)
        elemEventDropdown.style.visibility = "visible";
}

function hideEventDropdown(e) {
    var elemEventDropdown = document.getElementById("eventDropdown");
    var elemPageFrame = document.getElementById("pageFrame");

    if (elemEventDropdown && elemPageFrame) {
        if (e == undefined)
            e = window.event; // IE

        // convert mouse position reported against visible client area, to coordinates relative to page/document    
        var xPos = e.clientX + windowScrollLeft();
        var yPos = e.clientY + windowScrollTop();

        // get position of menu relative to page/document
        var menuTop = elemPageFrame.offsetTop + elemEventDropdown.offsetTop;
        var menuLeft = elemPageFrame.offsetLeft + elemEventDropdown.offsetLeft;
        var menuWidth = elemEventDropdown.offsetWidth;
        var menuHeight = elemEventDropdown.offsetHeight;

        // hide menu if within 2px fudge factor (left/rigt/bottom): if left out, the onmouseout reports being inside of menu, when leaving on left(IE), or right & bottom(FF/Opera)
        var edge = 2;
        var insideMenu = (xPos >= (menuLeft + edge) && xPos <= (menuLeft + menuWidth - edge)) &&
                         (yPos >= (menuTop + 0) && yPos <= (menuTop + menuHeight - edge));
        if (!insideMenu)
            elemEventDropdown.style.visibility = "hidden";
    }
}

/*
// set/replace key-value pair in current query string and reload page
(originally tested as 'onLocationChanged( elem )', but not tested in generalized form
function setQueryStringValueAndReload(key, value)
{
var questionMarkOffset = document.location.href.indexOf('?');
if( questionMarkOffset < 0 )
window.location = document.location + '?' + key + '=' + value;
else 
{   
// filter out existing location directives from querystring
var queryString = document.location.href.substring(questionMarkOffset+1)
var queryParams = queryString.split("&"); 
var newLocation = document.location.href.substring(0, questionMarkOffset);
for( var i = 0; i < queryParams.length; i++ )
{
if( queryParams[i].indexOf( key + '=' ) < 0 )
newLocation = newLocation + queryParams[i] + '&';
}
// and add new location directive
if( newLocation[newLocation.length-1] != '&' )
newLocation = newLocation + '?' + key + '=' + value; 
else    
newLocation = newLocation + key + '=' + value; 
window.location = newLocation;       
}    
}

// Use this in UI/WebControls/ChangeLocationJS.ascx, if switching from postback to URL reload
// To enable, set autopostback to false, and add onchange="onLocationChanged(this)"

function onLocationChanged( elem )
{
if( elem )
{
var selectedMarketarea = elem.options[elem.selectedIndex].value;
setQueryStringValueAndReload('location', selectedMarketarea);
}
}

*/

// Used in UI/WebControls/ChangeLocationJS.ascx:
function changeLocationToDropdown(locationlistID) {

    var currentLocation = document.getElementById("CurrentLocation");
    var locationDropDown = document.getElementById("LocationDropDown");
    var locationList = document.getElementById(locationlistID);
    if (currentLocation && locationDropDown) {
        currentLocation.style.display = "none";
        locationDropDown.style.display = "inline";
        locationList.selectedIndex = 0;
    }
}

function hideInterstitialPage() {
    //alert("hideInterstitialPage");
    var divIntersitialContainter = document.getElementById("divInterstitialContainer");
    if (divIntersitialContainter != null) {
        divIntersitialContainter.style.display = "none";

        for (var i = 0; i < g_controlIdsToUnhideWhenIntersitialPageIsClosed.length; i++) {
            var objectToShow = document.getElementById(g_controlIdsToUnhideWhenIntersitialPageIsClosed[i]);
            if (objectToShow != null) {
                var newClassname = objectToShow.className.replace("hiddenElement", "");
                objectToShow.className = newClassname;
            }
        }
    }

    breadCrumbsInit();
}


//http://simon.incutio.com/archive/2004/05/26/addLoadEvent#p-13
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}


addLoadEvent(OnPageLoad);

function SetExchangeSetPriceToolTips() {

	var islblLearnMoreEnabled = document.getElementById("ucSaleDetails_lblLearnMore");
	if (!islblLearnMoreEnabled)
	{
		islblLearnMoreEnabled = document.getElementById("ucSellTicketsSummary_lblLearnMore");
	}
    var refHelpListUntil = document.getElementById("refHelpListUntil");

    if (refHelpListUntil) {
        var $jvar = jQuery.noConflict();
        if (!islblLearnMoreEnabled) {
            $jvar('#refHelpListUntil').hide();
        }
        else {
            // Set Price >> Listing Expiration
            var exchangeToolTipListingGametime = document.getElementById("exchangeToolTipListingGametime");
            var exchangeToolTipListingGametimePast = document.getElementById("exchangeToolTipListingGametimePast");
            var exchangeToolTipListingGametimePlus = document.getElementById("exchangeToolTipListingGametimePlus");
            var exchangeToolTipListingGametimeDefault = document.getElementById("exchangeToolTipListingGametimeDefault");

            if (exchangeToolTipListingGametime) {
                //alert("exchangeToolTipListingGametime : " + exchangeToolTipListingGametime);
                $jvar('#refHelpListUntil').attr('rel', '#exchangeToolTipSetPriceGameTime');
            }
            else if (exchangeToolTipListingGametimePast) {
                //alert("exchangeToolTipListingGametimePast : " + exchangeToolTipListingGametimePast);
                $jvar('#refHelpListUntil').attr('rel', '#exchangeToolTipSetPriceGameTimePast');
            }
            else if (exchangeToolTipListingGametimePlus) {
                //alert("exchangeToolTipListingGamePlus : " + exchangeToolTipListingGametimePlus);
                $jvar('#refHelpListUntil').attr('rel', '#exchangeToolTipSetPriceGameTimePlus');
            }
            else if (exchangeToolTipListingGametimeDefault) {
                //alert("exchangeToolTipListingGameDefault : " + exchangeToolTipListingGametimeDefault);
                $jvar('#refHelpListUntil').attr('rel', '#exchangeToolTipSetPriceGameTimeDefault');
            }

            $jvar('#refHelpListUntil').cluetip({ topOffset: -150, leftOffset: 10, width: 300, tracking: true, local: true, showTitle: false, dropShadow: false, positionBy: 'fixed' });
        }
    }
}

